from django.shortcuts import HttpResponse
from .models import Article, Category
from django.utils.timezone import make_aware
from datetime import datetime, time


def index(request):
    # TODO: 查询条件
    # TODO: 1.exact  filed_exact=xxx 等价于 filed=xxx
    # TODO: 使用精确的=进行查找
    # article = Article.objects.filter(title__exact='hello world')
    # article = Article.objects.filter(title='hello world')
    # article = Article.objects.filter(title__exact=None)
    # article = Article.objects.filter(title=None)
    # print(article.query)
    # print(article)

    # TODO: 2.iexact
    # TODO: 使用like进行查找
    article = Article.objects.filter(title__iexact='hello world')
    print(article.query)
    print(article)
    return HttpResponse('success')


def index1(request):
    # TODO: 通过模型的.get返回的数据类型为模型 class
    # TODO: 不能通过query模式输出SQL语句
    article = Article.objects.get(pk=1)
    print(article)
    print(type(article))  # TODO: <class 'article.models.Article'>
    print(isinstance(article, Article))  # TODO: True
    # print(article.query)
    return HttpResponse('success')


def index2(request):
    # TODO: 查询条件
    # TODO；1.contains
    # TODO: 大小写敏感，判断某个字段是否包含了某个数据
    # result = Article.objects.filter(title__contains='hello')
    # print(result.query)
    # print(result)

    # TODO: 2.icontains
    # TODO: 大小写不敏感的匹配查询
    result = Article.objects.filter(title__icontains='Hello')
    print(result.query)
    print(result)
    return HttpResponse('success')


def index3(request):
    # TODO: 查询条件
    # TODO: in - 提取那些给定的field的值是否在给定的容器中。容器可以为list、tuple或者任何一个可以迭代的对象，包括QuerySet对象
    # TODO: 查询ID为1、2、3的文章----- list
    # aritcles = Article.objects.filter(id__in=[1, 2, 3])
    # print(aritcles.query)
    # for aritcle in aritcles:
    #     print(aritcle)

    # TODO: 查询ID为1、2、3的文章分类----- list
    # result = Category.objects.filter(articles__in=[1, 2, 3])
    # for category in result:
    #     print(category)
    # print(result.query)

    # TODO: 所有标题中换hello(不区分大小写)的文章分类----- QuerySet对象
    articles = Article.objects.filter(title__icontains='hello')
    print(type(articles))  # TODO: <class 'django.db.models.query.QuerySet'>
    categories = Category.objects.filter(articles__in=articles)
    for category in categories:
        print(category)
    print(categories.query)
    return HttpResponse('success')


def index4(request):
    # TODO: 查询条件
    # TODO: gt - 某个field的值要大于给定的值
    # articles = Article.objects.filter(id__gt=2)
    # print(articles)
    # print(articles.query)

    # TODO: gte - 类似于gt，是大于等于
    # articles = Article.objects.filter(id__gte=2)
    # print(articles)
    # print(articles.query)

    # TODO: lt - 类似于gt是小于
    # articles = Article.objects.filter(id__lt=3)
    # print(articles)
    # print(articles.query)

    # TODO: lte - 类似于lt，是小于等于
    articles = Article.objects.filter(id__lte=3)
    print(articles)
    print(articles.query)
    return HttpResponse('success')


def index5(request):
    # TODO: 查询操作
    # 1.startswith: 判断某个字段的值是否是以某个值开始的.大小写敏感.
    # articles = Article.objects.filter(title__startswith='Hello')
    # articles = Article.objects.filter(title__startswith='hello')
    # print(articles.query)
    # print(articles)

    # 2.istartswith：类似于startswith，但是大小写是不敏感的
    # articles = Article.objects.filter(title__istartswith='Hello')
    # print(articles.query)
    # print(articles)

    # 3.endswith：判断某个字段的值是否以某个值结束。大小写敏感
    # articles = Article.objects.filter(title__endswith='hello')
    # articles = Article.objects.filter(title__endswith='Hello')
    # print(articles.query)
    # print(articles)

    # 4.iendswith: 类似于endswith，只不过大小写不敏感
    articles = Article.objects.filter(title__iendswith='Hello')
    print(articles.query)
    print(articles)
    return HttpResponse('success')


def index6(request):
    # TODO: 查询条件
    # range：判断某个field的值是否在给定的区间中
    start_time = make_aware(datetime(year=2020, month=9, day=20, hour=10, minute=0, second=0))
    end_time = make_aware(datetime(year=2020, month=9, day=20, hour=16, minute=0, second=0))
    articles = Article.objects.filter(create_time__range=(start_time, end_time))
    print(articles.query)
    print(articles)
    return HttpResponse('success')


def index7(request):
    # TODO: 查询条件
    # 1.date: 针对某些date或者datetime类型的字段。可以指定date的范围。并且这个时间过滤，还可以使用链式调用
    # articles = Article.objects.filter(create_time__date=datetime(year=2020, month=9, day=20))

    # 2.year：根据年份进行查找
    # articles = Article.objects.filter(create_time__year=2020)

    # 3.month：同year，根据月份进行查找
    # articles = Article.objects.filter(create_time__month=9)

    # 4.day：同year，根据日期进行查找
    # articles = Article.objects.filter(create_time__day=21)

    # 5.week_day：根据星期几进行查找。1表示星期天，7表示星期六，2-6代表的是星期一到星期五
    # TODO: 查找星期天数据
    # articles = Article.objects.filter(create_time__week_day=1)

    # 6.time：根据时间进行查找
    start_time = time(hour=12, minute=56, second=42)
    end_time = time(hour=16, minute=0,second=0)
    articles = Article.objects.filter(create_time__time__range=(start_time, end_time))
    print(articles.query)
    print(articles)
    return HttpResponse('success')


def index8(request):
    # TODO: 查询条件
    # 1.isnull：根据值是否为空进行查找
    # TODO: 查找create_time为空数据
    # articles = Article.objects.filter(create_time__isnull=True)
    # TODO: 查找create_time不为空数据
    # articles = Article.objects.filter(create_time__isnull=False)

    # 2.regex：大小写敏感的正则表达式
    # articles = Article.objects.filter(title__regex=r'^Hello')

    # 3.iregex：大小写不敏感的正则表达式
    articles = Article.objects.filter(title__iregex=r'^Hello')
    print(articles.query)
    print(articles)
    return HttpResponse('success')